package com.xxxy.dongyexuan.action;

import com.xxxy.dongyexuan.mapper.IOrderMapper;
import com.xxxy.dongyexuan.po.Order;
import com.xxxy.dongyexuan.po.PageBean;
import com.xxxy.dongyexuan.service.impl.PayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;

@RestController
@CrossOrigin
@RequestMapping("orderInfo")
public class OrderAction {

    @Autowired
    private PayService payService;

    @Autowired
    private IOrderMapper mapper;

    //订单支付
    @PostMapping("/createOrderInfo")
    public String findByMeiRongPage(@RequestBody Order order) {

        order.setOrderId(UUID.randomUUID().toString().replace("-", "").toUpperCase());
        return payService.unifiedOrder(order);
    }

    //分页查询
    @GetMapping("/order/{pagenow}/{pagesize}")
    public PageBean findByOrderPage(@PathVariable("pagenow") int pagenow,
                                    @PathVariable("pagesize") int pagesize,
                                    @RequestParam("uid") int uid) {
        //开始条数
        int from = (pagenow - 1) * pagesize;
        //总条数
        int total = this.mapper.getcount(1);
        //计算总页数
        int pages = (total - 1) / pagesize + 1;
        List data = this.mapper.findByOrderPage(uid, from, pagesize);
        PageBean pageBean = new PageBean();
        pageBean.setPages(pages);
        pageBean.setPagenow(pagenow);
        pageBean.setPagesize(pagesize);
        pageBean.setTotal(total);
        pageBean.setData(data);
        return pageBean;
    }

}